package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.RequiredPermission;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//类似之前的Servlet
//请求用户的请求和相应

@Controller//IOC注解，将Controller交给Spring容器管理
@RequestMapping("/employee")//为当前的Controller指定一个资源路径
public class EmployeeController {

    @Autowired
    private IEmployeeService service;

    @Autowired
    private IRoleService roleService;


    @Autowired
    private IDepartmentService departmentService;


    @RequiredPermission(name="员工列表",expression="employee:list")
    //处理查询的请求


    //SpringMVC会自动将形成作为共享数据实现共享
    //名称默认为类型的首字母小写 employeeQueryObject
    //如果需要修改，可以使用@ModelAttribute
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        //查询到所有的部门
        PageResult result = service.query(qo);
        //将查询到的部门共享给页面
        model.addAttribute("result",result);

        model.addAttribute("depts",departmentService.listAll());

        //指定页面跳转的视图
        return "employee/list";
    }

    @RequiredPermission(name="员工删除",expression="employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        //从数据库中将对应id的部门删除
        service.delete(id);
        //重新访问list方法执行查询并跳转到list.jsp页面
        //默认的跳转方式是请求转发
        //使用redirect:前缀修改跳转的方式为重定向
        return "redirect:/employee/list.do";
    }

    @RequiredPermission(name="员工编辑",expression="employee:input")
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if(id !=null){
            Employee dept = service.get(id);
            model.addAttribute("entity", dept);
        }

        //查询出所有的部门，共享给input.jsp
        model.addAttribute("depts",departmentService.listAll());

        //查询出所有的角色，共享给input.jsp
        model.addAttribute("roles",roleService.listAll());

        //跳转到input.jsp页面
        return "employee/input";
    }

    @RequiredPermission(name="员工保存或者更新",expression="employee:saveOrUpdate")
    //执行保存或更新
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee e, Long[] ids){
        if(e.getId() !=null){
            //更新ee
            service.update(e,ids);
        }else{
            //保存
            service.save(e, ids);
        }

        return "redirect:/employee/list.do";
    }

    @RequiredPermission(name="员工批量删除",expression="employee:batchDelete")
    @RequestMapping("/batchDelete")
    public ModelAndView batchDelete(Long[] ids){
        service.batchDelete(ids);

        //异步请求不能再服务端实现跳转
        return null;
    }
    @RequiredPermission(name="员工批量删除",expression="employee:exportXls")
    @RequestMapping("/exportXls")
    public ModelAndView exportXls(HttpServletResponse resp) throws IOException {
        //告诉浏览器下载文件的信息
        resp.setHeader("Content-Disposition","attachment;filename=employee.xls");
        HSSFWorkbook workbook = service.exportXls();
        workbook.write(resp.getOutputStream());
        return null;
    }

    @RequiredPermission(name="员工导入",expression="employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
       service.importXLS(file.getInputStream());
        return "redirect:/employee/list.do";
    }
}
